package oop.lab.console.command;

import oop.lab.console.playground.PlayGround;
import oop.lab.console.playground.Player;
import oop.lab.domain.game.Game;

abstract public class PlacePieceCommand implements Command {
    private final Game game;
    private final PlayGround session;

    public PlacePieceCommand(Game game, PlayGround session) {
        this.game = game;
        this.session = session;
    }

    @Override
    public void execute(String args) {
        // 将1A或1a转换为00
        int x = args.charAt(0) - '1';
        int y = Character.toUpperCase(args.charAt(1)) - 'A';
        game.placePiece(x, y);
    }

    @Override
    public boolean canAccept(String input) {
        if (input.length() != 2)
            return false;
        char upperChar = Character.toUpperCase(input.charAt(1));
        return input.charAt(0) >= '1' && input.charAt(0) <= '9' &&
                upperChar >= 'A' && upperChar <= 'J';
    }

    @Override
    public String prompt() {
        //获取当前玩家
        Player currentPlayer = session.getPlayer(game.getCurrentPlayer());
        return String.format("请玩家[%s]输入落子位置(1A)", currentPlayer.getName());
    }

    @Override
    abstract public boolean isEnabled();

}
